 package com.hb.trade.server.center.sys.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.center.sys.dto.NewsCenterDTO;
import com.hb.core.pojo.center.sys.entity.NewsCenter;
import com.hb.core.pojo.enums.RoleSignEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.server.center.sys.manager.NewsCenterManager;
import com.hb.trade.server.center.sys.manager.news.NewsCenterSava;
import com.hb.trade.server.center.sys.service.NewsCenterService;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 消息中心 前端控制器
 * </p>
 *
 * @author zhongdl
 * @since 2022-02-18
 */
@RestController
public class NewsCenterController {

    protected final NewsCenterService newsCenterService;

    public NewsCenterController(NewsCenterService newsCenterService) {
        this.newsCenterService = newsCenterService;
    }

    /**
     *
     * @Description: 分页查询消息中心列表
     * @Author: zhongdl
     * @Date:2022/2/18 11:31
     * @param newsCenterDTO:
     * @return: {@link Message <  PageResult <  NewsCenterDTO >> }
     **/
    @PostMapping("/newsCenter/pageNewsCenter")
    public Message<PageResult<NewsCenterDTO>> pageNewsCenter(@RequestBody NewsCenterDTO newsCenterDTO){
        Page<NewsCenter> pages = newsCenterDTO.pages();
        QueryWrapper<NewsCenter> wrapper = NewsCenter.wrapper();
        wrapper.eq("receiver_id",newsCenterDTO.getReceiverId())
                .eq(newsCenterDTO.getNewsType() != null,"news_type",newsCenterDTO.getNewsType())
                .eq(newsCenterDTO.getReadFlag() != null,"read_flag",newsCenterDTO.getReadFlag())
                .in(Enums.equals(RoleSignEnum.TENDEREE,newsCenterDTO.getRoleSign()),"news_type_two",CollUtil.list(false,3,4,6,8,9,13))
                .in(Enums.equals(RoleSignEnum.BIDDER,newsCenterDTO.getRoleSign()),"news_type_two",CollUtil.list(false,1,2,3,5,7,10,11,12,14))
                .in(Enums.equals(RoleSignEnum.AGENCY,newsCenterDTO.getRoleSign()),"news_type_two",CollUtil.list(false,3,4,6,8,9,13))
                .orderByDesc("create_time");
        newsCenterService.page(pages,wrapper);
        PageResult<NewsCenterDTO> newsCenterDTOPageResult = PageResult.get(pages).transferTo(NewsCenterDTO.class);
        return MsgUtil.success(newsCenterDTOPageResult);
    }

    /**
     *
     * @Description: 新增一条消息
     * @Author: zhongdl
     * @Date:2022/2/18 13:31
     * @param newsCenterDTO:
     * @return: {@link Message< PageResult< NewsCenterDTO>> }
     **/
    @PostMapping("/newsCenter/insertNewsCenter")
    public Message<Boolean> insertNewsCenter(@RequestBody NewsCenterDTO newsCenterDTO){
        NewsCenterManager newsCenterManager = newsCenterDTO.to(NewsCenterManager.class);
        NewsCenterSava newsCenterSava = newsCenterManager.getNewsCenterSava();

        boolean saveFlag = newsCenterSava.insertNewsCenter(newsCenterManager);
        return MsgUtil.success(saveFlag);
    }

    /**
     *
     * @Description: 批量标记为已读
     * @Author: zhongdl
     * @Date:2022/2/18 14:58
     * @param newsCenter:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/newsCenter/updateBatchRead")
    public Message<Boolean> updateBatchRead(@RequestBody NewsCenterDTO newsCenter){
        if (CollUtil.isEmpty(newsCenter.getIds())){
            return MsgUtil.success(true);
        }
        UpdateWrapper<NewsCenter> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("read_flag", LogicEnum.YES.getCode())
                .in("id",newsCenter.getIds());
        boolean update = newsCenterService.update(updateWrapper);
        return MsgUtil.success(update);
    }


    /**
     *
     * @Description: 批量删除消息
     * @Author: zhongdl
     * @Date:2022/2/18 15:25
     * @param newsCenter:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/newsCenter/removeBatch")
    public Message<Boolean> removeBatch(@RequestBody NewsCenterDTO newsCenter){
        if (CollUtil.isEmpty(newsCenter.getIds())){
            return MsgUtil.success(true);
        }
        UpdateWrapper<NewsCenter> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("delete_flag", LogicEnum.YES.getCode())
                .in("id",newsCenter.getIds());
        boolean update = newsCenterService.update(updateWrapper);
        return MsgUtil.success(update);
    }

    /**
     *
     * @Description: 获得已读和未读的消息个数
     * @Author: zhongdl
     * @Date:2022/2/18 17:07
     * @param newsCenter:
     * @return: {@link Message< NewsCenterDTO> }
     **/
    @PostMapping("/newsCenter/readCount")
    public Message<NewsCenterDTO> readCount(@RequestBody NewsCenterDTO newsCenter){
        long readCount = newsCenterService.readFlagCount(newsCenter.getReceiverId(),LogicEnum.YES.getCode(),newsCenter.getRoleSign());
        long totalCount = newsCenterService.readFlagCount(newsCenter.getReceiverId(),null,newsCenter.getRoleSign());
        //已读
        newsCenter.setReadCount(readCount);
        //未读
        newsCenter.setUnreadCount(totalCount - readCount);
        return MsgUtil.success(newsCenter);

    }
}
